(function() {

	var appModule = angular.module('tracecashModule');

	appModule.controller('SinisterLetterListCtrl', [ '$scope', '$rootScope', '$routeParams', '$http', '$modal', function($scope, $rootScope, $routeParams, $http, $modal) {
		$scope.sinisterId = $routeParams.sinisterId;
		$scope.generate = function() {
			var modalInstance = $modal.open({
				templateUrl : 'html/letter/letter-detail.html',
				controller : 'SinisterLetterDetailCtrl',
				size : 'lg',
				resolve : {
					sinisterId : function() {
						return $scope.sinisterId;
					}
				}
			});
			modalInstance.result.then(function(message) {
				$scope.message = message;
				$scope.setPage(1);
			}, function() {
			});
		};
		$scope.searchResults = {};
		$scope.setPage = function(page) {
			$scope.currentPage = page;
			$http.get($rootScope.REST_PATH + '/letter/search/' + $routeParams.sinisterId, {
				params : {
					p : $scope.currentPage,
					n : $rootScope.PAGE_SIZE
				}
			}).success(function(data) {
				$scope.searchResults = data;
			});
		};
		$scope.setPage(1);
	} ]);

	appModule.directive('sinisterPersonList', function() {
		return {
			restrict : 'AE',
			template : '<select class="form-control" ng-model="ngModel" ng-options="(person.idCardNumber + \' - \' + person.name) for person in people"></select>',
			require : '^ngModel',
			controller : function($scope, $rootScope, $http) {
				$http.get($rootScope.REST_PATH + '/person/findBySinister/' + $scope.sinisterId).success(function(people) {
					$scope.people = people;
					$scope.ngModel = people[0];
				});
			},
			scope : {
				ngModel : '=',
				sinisterId : '='
			}
		};
	});

	appModule.controller('SinisterLetterDetailCtrl', [
			'$scope',
			'$rootScope',
			'$http',
			'$modalInstance',
			'sinisterId',
			function($scope, $rootScope, $http, $modalInstance, sinisterId) {
				$scope.sinisterId = sinisterId;
				$scope.save = function() {
					$http.post($rootScope.REST_PATH + '/letter/generateFreeText/' + sinisterId + '/' + $scope.action.letterTarget.id, $scope.action.freeText).success(
							function(message) {
								if (message.code == $rootScope.CODE_SUCCESS) {
									$modalInstance.close(message);
								} else {
									$scope.message = message;
								}
							});
				};
				$scope.cancel = function() {
					$modalInstance.dismiss('cancel');
				};
			} ]);

	appModule.directive('fileModel', [ '$parse', function($parse) {
		return {
			restrict : 'A',
			link : function(scope, element, attrs) {
				var model = $parse(attrs.fileModel);
				var modelSetter = model.assign;
				element.bind('change', function() {
					scope.$apply(function() {
						modelSetter(scope, element[0].files[0]);
						scope.fileName = element[0].files[0].name;
					});
				});
			}
		};
	} ]);

	appModule.controller('UploadFileCtrl', [ '$scope', '$rootScope', '$http', function($scope, $rootScope, $http) {
		$scope.fileName = null;
		$scope.uploadFile = function() {
			var fd = new FormData();
			fd.append('file', $scope.myFile);
			$http.post($rootScope.REST_PATH + '/file/upload/' + $scope.sinisterId, fd, {
				transformRequest : angular.identity,
				headers : {
					'Content-Type' : undefined
				}
			}).success(function(message) {
				$scope.message = message;
				$scope.fileName = null;
				$scope.setPage(1);
			});
		};
	} ]);

})();